package com.top.jwtdemo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author  Cjj
 * @data  2024/6/17 11:42
 * @blog  定义跨越拦截器
 */
public class CrossInterceptorHandler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler){
        //setHeader是设置响应头  "key" "value"
        // *是所有 也可以写具体一个  比如说前端的ip
        response.setHeader("Access-Control-Allow-Origin","*");
        //true 允许传递cookie
        response.setHeader("Access-Control-Allow-Credentials","true");
        //或者* 代表所有
        response.setHeader("Access-Control-Allow-Methods","POST,GET,PUT,OPTIONS");
        //控制过期最大时间 为3600秒
        response.setHeader("Access-Control-Max-Age","3600");
        //允许的请求头
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,accept,authorization,content-type");
         return true; //放行
    }
}
